﻿using FileTransfer.Client.Http.Client;

// ReSharper disable once CheckNamespace
namespace FileTransfer.Client.Http.Api;

public sealed partial class FileControllerApi
{
    public async Task<string> UploadChunkStreamAsync(
        string md5,
        string chunkMd5,
        int chunkIndex,
        long chunkOffset,
        FileParameter body,
        CancellationToken cancellationToken = default(CancellationToken)
    )
    {
        var localVarResponse = await UploadChunkStreamWithHttpInfoAsync(
                md5,
                chunkMd5,
                chunkIndex,
                chunkOffset,
                body,
                cancellationToken
            )
            .ConfigureAwait(false);
        return localVarResponse.Data;
    }

    public async Task<ApiResponse<string>> UploadChunkStreamWithHttpInfoAsync(
        string md5,
        string chunkMd5,
        int chunkIndex,
        long chunkOffset,
        FileParameter body,
        CancellationToken cancellationToken = default(CancellationToken)
    )
    {
        // verify the required parameter 'md5' is set
        if (md5 == null)
            throw new ApiException(
                400,
                "Missing required parameter 'md5' when calling FileControllerApi->UploadChunk"
            );

        // verify the required parameter 'chunkMd5' is set
        if (chunkMd5 == null)
            throw new ApiException(
                400,
                "Missing required parameter 'chunkMd5' when calling FileControllerApi->UploadChunk"
            );

        // verify the required parameter 'body' is set
        if (body == null)
            throw new ApiException(
                400,
                "Missing required parameter 'body' when calling FileControllerApi->UploadChunk"
            );

        var localVarRequestOptions = new RequestOptions();

        var contentTypes = new[] { "application/json" };

        // to determine the Accept header
        var accepts = new[] { "*/*" };

        var localVarContentType = ClientUtils.SelectHeaderContentType(contentTypes);
        if (localVarContentType != null)
            localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);

        var localVarAccept = ClientUtils.SelectHeaderAccept(accepts);
        if (localVarAccept != null)
            localVarRequestOptions.HeaderParameters.Add("Accept", localVarAccept);

        localVarRequestOptions.PathParameters.Add("md5", ClientUtils.ParameterToString(md5)); // path parameter
        localVarRequestOptions.QueryParameters.Add(
            ClientUtils.ParameterToMultiMap("", "chunkMd5", chunkMd5)
        );
        localVarRequestOptions.QueryParameters.Add(
            ClientUtils.ParameterToMultiMap("", "chunkIndex", chunkIndex)
        );
        localVarRequestOptions.QueryParameters.Add(
            ClientUtils.ParameterToMultiMap("", "chunkOffset", chunkOffset)
        );
        localVarRequestOptions.Data = body;

        // make the HTTP request

        var localVarResponse = await AsynchronousClient
            .PostAsync<string>(
                "/api/files/upload/{md5}/chunk",
                localVarRequestOptions,
                Configuration,
                cancellationToken
            )
            .ConfigureAwait(false);

        if (ExceptionFactory != null)
        {
            var exception = ExceptionFactory("UploadChunk", localVarResponse);
            if (exception != null)
                throw exception;
        }

        return localVarResponse;
    }
}
